package com.example.administrator.xiaobo.fragment.LiveDetailsFragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.xiaobo.R;
import com.example.administrator.xiaobo.adapter.ShowRecyclerAdapter;
import com.example.administrator.xiaobo.bean.show_1Bean;
import com.example.administrator.xiaobo.bean.show_1_itemBean;
import com.example.administrator.xiaobo.bean.show_2Bean;
import com.example.administrator.xiaobo.bean.show_2_itemBean;
import com.example.administrator.xiaobo.bean.show_3Bean;
import com.example.administrator.xiaobo.bean.show_3_itemBean;
import com.example.administrator.xiaobo.bean.show_5Bean;
import com.example.administrator.xiaobo.bean.show_5_itemBean;
import com.example.administrator.xiaobo.bean.show_6Bean;
import com.example.administrator.xiaobo.bean.show_6_itemBean;
import com.example.administrator.xiaobo.bean.show_9Bean;
import com.example.administrator.xiaobo.bean.show_9_itemBean;
import com.example.administrator.xiaobo.bean.show_Bean;
import com.example.administrator.xiaobo.jsonbean.show_1JsonBean;
import com.example.administrator.xiaobo.jsonbean.show_2JsonBean;
import com.example.administrator.xiaobo.jsonbean.show_3JsonBean;
import com.example.administrator.xiaobo.jsonbean.show_4JsonBean;
import com.example.administrator.xiaobo.jsonbean.show_5JsonBean;
import com.example.administrator.xiaobo.jsonbean.show_6JsonBean;
import com.example.administrator.xiaobo.jsonbean.show_9JsonBean;
import com.example.administrator.xiaobo.url.URLAdress;
import com.example.administrator.xiaobo.utils.AsyncTaskHelper;
import com.example.administrator.xiaobo.utils.JsonHelper;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowFragment extends Fragment {

    private AsyncTaskHelper helper;
    private View view;
    private RecyclerView recyclerView;
    private ShowRecyclerAdapter adapter;
    private LinkedList<show_Bean> data;

    public ShowFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_show,null);
        init();
        loadURL();

    }

    private void init() {
        recyclerView = (RecyclerView) view.findViewById(R.id.show_recyclerView);
        data = new LinkedList<>();
        adapter = new ShowRecyclerAdapter(getContext(),data);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        helper = new AsyncTaskHelper();
    }

    private void loadURL() {

        helper.downloadData(URLAdress.SHOW_URL, new AsyncTaskHelper.OnDataDownloadListener() {
            @Override
            public void onDataDownload(byte[] result) {
                String s = new String(result);
                Log.e(TAG, "onDataDownload: "+s);
                Log.e(TAG, "onDataDownload: "+URLAdress.SHOW_URL);
                parseJson(s);
            }
        });
    }

    private void parseJson(String s) {
        int viewType;
        String []datas = JsonHelper.jsonStringToArray(s,"data");
        for (String bean:datas) {
            try {
                JSONObject object = new JSONObject(bean);
                viewType =  object.getInt("viewtype");
                parseJson(viewType,bean);//根据不同的viewType解析不同的jsonObject
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void parseJson(int viewType,String bean) {
        Gson gson = new Gson();
        switch(viewType){
            case 1:
                show_1JsonBean show_1JsonBean = gson.fromJson(bean, show_1JsonBean.class);
                show_1Bean show_1Bean = new show_1Bean();
                show_1Bean.viewType = show_1JsonBean.getViewtype();
                for (int i = 0; i < show_1JsonBean.getItemdata().size(); i++) {
                    show_1_itemBean show_1_itemBean = new show_1_itemBean();
                    show_1_itemBean.id = show_1JsonBean.getItemdata().get(i).getId();
                    show_1_itemBean.level1 = show_1JsonBean.getItemdata().get(i).getIcon().getLevel1();
                    show_1_itemBean.name = show_1JsonBean.getItemdata().get(i).getName();
                    show_1_itemBean.recordparams = show_1JsonBean.getItemdata().get(i).getRecordparams();
                    show_1Bean.list.add(show_1_itemBean);
                }
                data.add(show_1Bean);
                break;
            case 2:
                show_2JsonBean show_2JsonBean = gson.fromJson(bean, show_2JsonBean.class);
                show_2Bean show_2Bean = new show_2Bean();
                show_2Bean.viewType = show_2JsonBean.getViewtype();
                for (int i = 0; i < show_2JsonBean.getItemdata().size(); i++) {
                    show_2_itemBean show_2_itemBean = new show_2_itemBean();
                    show_2_itemBean.code = show_2JsonBean.getItemdata().get(i).getCode();
                    show_2_itemBean.level1 = show_2JsonBean.getItemdata().get(i).getImage().getLevel1();
                    show_2_itemBean.name = show_2JsonBean.getItemdata().get(i).getName();
                    show_2_itemBean.recordparams = show_2JsonBean.getItemdata().get(i).getRecordparams();
                    show_2Bean.list.add(show_2_itemBean);
                }
                data.add(show_2Bean);
                break;
            case 3:
                show_3JsonBean show_3JsonBean = gson.fromJson(bean, show_3JsonBean.class);
                show_3Bean show_3Bean = new show_3Bean();
                show_3Bean.viewType = show_3JsonBean.getViewtype();
                for (int i = 0; i < show_3JsonBean.getItemdata().size(); i++) {
                    show_3_itemBean show_3_itemBean = new show_3_itemBean();
                    show_3_itemBean.id = show_3JsonBean.getItemdata().get(i).getId();
                    show_3_itemBean.tag = show_3JsonBean.getItemdata().get(i).getTag();
                    show_3_itemBean.recordparams = show_3JsonBean.getItemdata().get(i).getRecordparams();
                    show_3Bean.list.add(show_3_itemBean);
                }
                data.add(show_3Bean);
                break;
            case 4://上方的轮播
                gson.fromJson(bean, show_4JsonBean.class);
                break;
            case 5:
                show_5JsonBean show_5JsonBean = gson.fromJson(bean, show_5JsonBean.class);
                show_5Bean show_5Bean = new show_5Bean();
                show_5Bean.viewType = show_5JsonBean.getViewtype();
                for (int i = 0; i < show_5JsonBean.getItemdata().size(); i++) {
                    show_5_itemBean show_5_itemBean = new show_5_itemBean();
                    show_5_itemBean.id = show_5JsonBean.getItemdata().get(i).getId();
                    show_5_itemBean.icon_level1 = show_5JsonBean.getItemdata().get(i).getLiveplatform().getIcon().getLevel1();
                    show_5_itemBean.level1 = show_5JsonBean.getItemdata().get(i).getListicon().getLevel1();
                    show_5_itemBean.recordparams = show_5JsonBean.getItemdata().get(i).getRecordparams();
                    show_5_itemBean.name = show_5JsonBean.getItemdata().get(i).getName();
                    show_5Bean.list.add(show_5_itemBean);
                }
                data.add(show_5Bean);
                break;
            case 6:
                show_6JsonBean show_6JsonBean = gson.fromJson(bean, show_6JsonBean.class);
                show_6Bean show_6Bean = new show_6Bean();
                show_6Bean.id = show_6JsonBean.getId();
                show_6Bean.level1 = show_6JsonBean.getIcon().getLevel1();
                show_6Bean.name = show_6JsonBean.getName();
                show_6Bean.viewType = show_6JsonBean.getViewtype();
                for (int i = 0; i < show_6JsonBean.getItemdata().size(); i++) {
                    show_6_itemBean show_6_itemBean = new show_6_itemBean();
                    show_6_itemBean.id = show_6JsonBean.getItemdata().get(i).getId();
                    show_6_itemBean.icon_level1 = show_6JsonBean.getItemdata().get(i).getLiveplatform().getIcon().getLevel1();
                    show_6_itemBean.level1 = show_6JsonBean.getItemdata().get(i).getListicon().getLevel1();
                    show_6_itemBean.name = show_6JsonBean.getItemdata().get(i).getName();
                    show_6_itemBean.recordparams = show_6JsonBean.getItemdata().get(i).getRecordparams();
                    show_6Bean.list.add(show_6_itemBean);
                }
                data.add(show_6Bean);
                break;
            case 9:
                show_9JsonBean show_9JsonBean = gson.fromJson(bean, show_9JsonBean.class);
                show_9Bean show_9Bean = new show_9Bean();
                show_9Bean.viewType = show_9JsonBean.getViewtype();
                for (int i = 0; i < show_9JsonBean.getItemdata().size(); i++) {
                    show_9_itemBean show_9_itemBean = new show_9_itemBean();
                    show_9_itemBean.id = show_9JsonBean.getItemdata().get(i).getId();
                    show_9_itemBean.level1 = show_9JsonBean.getItemdata().get(i).getListicon().getLevel1();
                    show_9_itemBean.name = show_9JsonBean.getItemdata().get(i).getName();
                    show_9_itemBean.sourcename = show_9JsonBean.getItemdata().get(i).getSourcename();
                    show_9_itemBean.times = show_9JsonBean.getItemdata().get(i).getTimes();
                    show_9_itemBean.title = show_9JsonBean.getItemdata().get(i).getTitle();
                    show_9_itemBean.viewnum = show_9JsonBean.getItemdata().get(i).getViewnum();
                    show_9_itemBean.streamurl = show_9JsonBean.getItemdata().get(i).getStreamurl();
                    show_9Bean.list.add(show_9_itemBean);
                }
                data.add(show_9Bean);
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return view;
    }


}
